import BigImg from '../../../components/BigImg-pc/index.vue'
import Util from 'assets/js/util'
export default {
  data () {
    return {
      /*--------待审核详情-------*/
      //待审核详情
      PendingAuditDetail: {
        convId: "",
        mobile: "",
        content: '',
        createTime: "",
        location: "",
        photos: []
      },
      //审核配置项
      rejectReasonList: [],
      integralMattersList: [],
      dealTypeList: [],
      report: { //处理
        auditStatus: '',
        dealType: '',
        id: '',
        integralMatters: '',
        rejectReason: '',
        otherRejectReason: '',
        msgContent: ''
      },
      isAudit: 1,
      isConfirm: false,
      otherReason: '',
      auditStatusName: '',
      //照片放大
      imgIndex: -1,
      imgSwitch: false,
      isRectangle: false,
      viewNotify: false,//通知预览modal开关
      isAsc:false,
      showWarnModal:false, //格式校验提示框
      warnContent:''  //提示文案
    }
  },
  mounted () {
    this.init();
  },
  watch: {
    // 如果路由有变化，会再次执行该方法
    '$route': 'init',
    'auditStatusName': function (val) {
      if (val == '未通过审核') {
        this.isAudit = 2
      } else if (val == '审核通过') {
        this.isAudit = 3
      }
    }
  },
  methods: {
    init: function () {
      let routeName = this.$route.name;
      if (routeName == "auditContent") {
        Util.setTitle('内容审核');
        this.report.id = this.$route.params.id
        this.isAsc = this.$route.params.isAsc
        this.queryPendingAuditDetail();
        this.auditConfig();
      }
    },

    /**
     *  获取待审核详情
     * */
    queryPendingAuditDetail () {
      var param = {
        id: this.$route.params.id
      }, _this = this;
      this.$http.post('convphotoManage/queryPendingAuditDetailById',param)
      //this.$http.get('/static/data/manage-getPendingById.json', param)
        .then(function (resData) {
          if (resData.meta.code == '0') {
            let data = resData.data;
            _this.PendingAuditDetail = data;
          } else {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: resData.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = resData.meta.message;
          }

        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })
    },
    /**
     *  获取下一个未处理的详情
     * */
    queryPendingAuditDetailByNext () {
      let _this = this;
      if (_this.comCheck()) {
        this.report.auditStatus = this.auditStatusName == '审核通过' ? 2 : 1;
        this.$http.post('convphotoManage/addAudit', this.report)
          .then(function (resData) {
            if(resData.meta.code == '0'){
              _this.viewNotify = false;
              let params={
                isAsc:_this.isAsc ? 1 : 2
              }
              _this.$http.post('convphotoManage/queryPendingAuditDetailByNext',params)
                .then(function (resData) {
                  if (resData.data) {
                    _this.$Notice.success({
                      title: '温馨提示',
                      desc: '操作成功',
                      duration:2,
                    });
                    _this.$nextTick(function () {
                      _this.PendingAuditDetail = resData.data;
                      _this.initConfig(resData.data.convId);
                    })
                  } else {
                    // _this.$Modal.warning({
                    //   title: '温馨提示',
                    //   content: '没有待审核的数据了'
                    // });
                    _this.showWarnModal = true;
                    _this.warnContent = '没有待审核的数据了';
                    setTimeout(function () {
                      _this.$router.go(-1)
                    }, 600)
                  }
                })
                .catch(function (err) {
                  // _this.$Modal.warning({
                  //   title: '温馨提示',
                  //   content: err.meta.message
                  // });
                  _this.showWarnModal = true;
                  _this.warnContent = err.meta.message;
                })
            }else{
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: resData.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = resData.meta.message;
            }
          })
          .catch(function (err) {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: err.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      }
      //处理
      //_this.report.auditStatus='';
      //_this.report.id=_this.PendingAuditDetail.convId;
      //_this.report.rejectReason='';
      //_this.report.otherRejectReason='';


    },
    /**
     * 表单校验
     */
    comCheck(){
      let _this = this, flag = true;
      // addBy lixu  置空原因
      if(this.viewNotify&&!this.report.msgContent.trim()){
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '消息内容不能为空'
        // });
        this.showWarnModal = true;
        this.warnContent =  '消息内容不能为空';
        return false;
      }
      if(this.viewNotify&&Util.isEmojiCharacter(this.viewNotify)){
        // this.$Modal.warning({
        //   title:'温馨提示',
        //   content:"消息内容不能包含emoji表情"
        // });
        this.showWarnModal = true;
        this.warnContent =  "消息内容不能包含emoji表情";
        return false;
      }
      _this.report.rejectReason = '';
      if (!this.auditStatusName) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '请选择审核结果'
        // });
        _this.showWarnModal = true;
        _this.warnContent = '请选择审核结果';
        return false;
      }
      this.report.auditStatus = this.auditStatusName == '审核通过' ? 2 : 1;
      if (this.report.auditStatus == 1) {
        this.rejectReasonList.forEach(function (item) {
          if (item.checked) {
            _this.report.rejectReason += item.dictCode + ',';
            if (item.dictValue == '其他') {
              if (!_this.otherReason.trim()) {
                // _this.$Modal.warning({
                //   title: '温馨提示',
                //   content: '请填写其他原因'
                // });
                _this.showWarnModal = true;
                _this.warnContent = '请填写其他原因';
                flag = false;
              } else if (_this.otherReason.trim().length > 50) {
                // _this.$Modal.warning({
                //   title: '温馨提示',
                //   content: '处理途径长度不超过50字'
                // });
                _this.showWarnModal = true;
                _this.warnContent = '处理途径长度不超过50字';
                flag = false;
              } else if(Util.isEmojiCharacter(_this.otherReason)){
                // _this.$Modal.warning({
                //   title: '温馨提示',
                //   content: '处理途径不能包含emoji表情'
                // });
                _this.showWarnModal = true;
                _this.warnContent = '处理途径不能包含emoji表情';
                flag = false;
              }else {
                _this.report.otherRejectReason = _this.otherReason
              }
            }
          }
        });
        _this.report.rejectReason = _this.report.rejectReason.slice(0, -1)
        if (!this.report.rejectReason && !this.report.otherRejectReason) {
          // this.$Modal.warning({
          //   title: '温馨提示',
          //   content: '请选择退回原因'
          // });
          _this.showWarnModal = true;
          _this.warnContent = '请选择退回原因';
          return false;
        }
      }

      if (this.report.auditStatus == 2) {
        if (!this.report.dealType) {
          // this.$Modal.warning({
          //   title: '温馨提示',
          //   content: '请选择处理方式'
          // });
          _this.showWarnModal = true;
          _this.warnContent = '请选择处理方式';
          return false;
        }

      }
      return flag;
    },
    /**
     * 初始化配置参数
     * @param id
     */
    initConfig(id){
      for (var i = 0; i < this.rejectReasonList.length; i++) {
        this.rejectReasonList[i].checked = false;
      }
      this.report = { //处理
        auditStatus: '',
        dealType: '',
        id: id,
        integralMatters: '',
        rejectReason: '',
        otherRejectReason: ''
      };
      this.isAudit = 1;
      this.isConfirm = false;
      this.otherReason = '';
      this.auditStatusName = ''
    },
    /**
     * 返回未审核列表
     */
    goAuditList(){
      let param = {
        convId: this.report.id
      }, _this = this;
      this.$http.post('convphotoManage/unlockAuditRecord', param)
        .then(function (data) {
          if (data.meta.code == '0') {
            _this.$router.push({name: 'notAuditedList'})
          } else {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: data.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = data.meta.message;
          }

        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })

    },
    //查询专题
    goToSubject: function(item){
      this.$router.push({
        name: "subjectDetail",
        query:{
          subjectId:item.subjectId
        }
      })
    },
    /**
     *  获取审核配置项
     * */
    auditConfig () {
      let _this = this;
      this.$http.post('convphotoManage/auditConfig')
      //this.$http.get('/static/data/manage-auditConfig.json')
        .then(function (resData) {
          let data = resData.data;
          for (var i = 0; i < data.rejectReasonList.length; i++) {
            data.rejectReasonList[i].checked = false;
          }
          _this.rejectReasonList = data.rejectReasonList || [];
          _this.integralMattersList = data.integralMattersList || [];
          _this.dealTypeList = data.dealTypeList || [];
        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
		  console.log('err', err);
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })

    },
    //checkbox开关
    select(item) {
      item.checked = !item.checked;
    },
    /**
     * 放大图片
     * @param index
     */
    bigImg(index){
      this.imgIndex = index
      this.imgSwitch = true
    },
    reset(){
      this.imgIndex = -1
    },
    /**
     * 查看通知消息
     */
    viewNotifyInfo(){
      let _this = this;
      let flag = _this.comCheck();
      if (flag) {
        console.log(this.isAsc ? 1 : 2);
        //获取通知详情
        this.$http.post('convphotoManage/queryMsgContentBeforeAudit', this.report)
          .then(function (data) {
            if (data.meta.code == '0') {
              _this.viewNotify = true;
              _this.report.msgContent = data.data || '';
              _this.$nextTick(function(){
                document.querySelector('.view-notify-content').scrollTop = 0
              })
            } else {
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: data.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = data.meta.message;
            }
          })
          .catch(function (err) {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: err.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      }
    },
    closeModal(){
      this.showWarnModal = false;
    }
  },
  components: {
    'big-img': BigImg
  }

}
